package com.example.coffeeseller_2.model.helper;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.view.View;

import com.example.coffeeseller_2.model.factory.ChangeLayoutState;
import com.example.coffeeseller_2.model.helper.view_helper.CheckTipBeforeMakeViewHelper;
import com.example.coffeeseller_2.model.helper.view_helper.MakingViewHelper;
import com.example.coffeeseller_2.model.helper.view_helper.QuebiemaTasteViewHelper;
import com.example.coffeeseller_2.model.helper.view_helper.SaomaViewHelper;
import com.example.coffeeseller_2.model.helper.view_helper.StaticViewHelper;

/**
 * Created by WH on 2018/8/11.
 * 切换布局辅助类
 */

public class ChangeLayoutHelper {

    private static String TAG = "ChangeLayoutHelper";
    private MakingViewHelper makingViewHelper;
    private SaomaViewHelper saomaViewHelper;
    private StaticViewHelper staticViewHelper;
    private CheckTipBeforeMakeViewHelper checkTipBeforeMakeViewHelper;
    private QuebiemaTasteViewHelper quebiemaTasteViewHelper;
    private View view;
    private Handler handler;
    private Context context;
    private ChangeLayoutState curViewState = ChangeLayoutState.LOADING_DATA;

    public ChangeLayoutHelper(Context context) {
        this.context = context;
        init();
    }

    public View getViewByState(ChangeLayoutState state, Bitmap bitmap) {
        curViewState = state;
        changeView(bitmap);
        return view;
    }


    public MakingViewHelper getMakingViewHelper() {
        return makingViewHelper;
    }

    public SaomaViewHelper getSaomaViewHelper() {
        return saomaViewHelper;
    }

    public StaticViewHelper getStaticViewHelper() {
        return staticViewHelper;
    }

    public QuebiemaTasteViewHelper getQuebiemaTasteViewHelper() {
        return quebiemaTasteViewHelper;
    }

    public CheckTipBeforeMakeViewHelper getCheckTipBeforeMakeViewHelper() {
        return checkTipBeforeMakeViewHelper;
    }

    public ChangeLayoutState getCurViewState() {
        return curViewState;
    }

    private void init() {
        handler = new Handler();
        makingViewHelper = new MakingViewHelper(handler, context);
        saomaViewHelper = new SaomaViewHelper(context);
        staticViewHelper = new StaticViewHelper(context);
        checkTipBeforeMakeViewHelper = new CheckTipBeforeMakeViewHelper(context);
        quebiemaTasteViewHelper = new QuebiemaTasteViewHelper(context);
    }

    private void changeView(Bitmap bitmap) {

        switch (curViewState) {
            case MACHINE_TIP:

                initView();
                view = checkTipBeforeMakeViewHelper.getView();
                if (view != null) {
                    checkTipBeforeMakeViewHelper.startAnim();
                }
                break;
            case LOADING_DATA:

                initView();
                view = staticViewHelper.getLoadingView();

                break;
            case SAOMA:
                view = saomaViewHelper.getView(bitmap);
                break;
            case SAOMA_HAS_ERR:
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        saomaViewHelper.isShowRequestQRcode(true);
                    }
                });

                break;
            case SAOMA_SUCCESS:
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        saomaViewHelper.setSaomaResult(true);
                    }
                });

                break;
            case PAY_SUCCESS:
                initView();
                view = staticViewHelper.getPaySuccessView();

                break;
            case PAY_FAILED:
                initView();
                view = staticViewHelper.getPayFailureView();
                break;
            case WAIT_MAKING:
                initView();
                view = makingViewHelper.getView();
                break;
            case MAKING_FINISH:
                initView();
                view = staticViewHelper.getMakeFinishView();

                break;
            case MAKING_FAILED:
                initView();
                view = staticViewHelper.getMakingFailedView();

                break;
            case TAKECUP_SUCCESS:
                initView();
                view = staticViewHelper.getTakeCupSuccessView();

                break;
            case QUBEIMA_TASTE:

                initView();
                view = quebiemaTasteViewHelper.getView();
                break;
        }
    }

    /*
    * 将view回到初始状态
    * */
    private void initView() {
        saomaViewHelper.clear();
        makingViewHelper.clear();
        staticViewHelper.clear();
        checkTipBeforeMakeViewHelper.clear();
    }
}
